import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DialogTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DialogTest();
  }
}

class _DialogTest extends State<DialogTest> {
  TextEditingController _controller = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  bool _isSave = false;
  PersistentBottomSheetController<String> _persistentBottomSheetController;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("dialog测试"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              color: Colors.deepOrange,
              child: Text("dialog1"),
              shape: RoundedRectangleBorder(
                  side: BorderSide(), borderRadius: BorderRadius.circular(30)),
              onPressed: () {
                showDialog(
                    context: context,
                    barrierDismissible: true,
                    child: AlertDialog(
                      title: Text('登录'),
                      content: Center(
                        heightFactor: 1,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            TextField(
                              controller: _controller,
                              decoration: InputDecoration(
                                  prefixIcon: Icon(Icons.accessibility),
                                  border: OutlineInputBorder(),
                                  labelText: '请输入用户名',
                                  hintText: '请输入用户名或手机号',
                                  contentPadding: EdgeInsets.all(0)),
                              autofocus: true,
                              textInputAction: TextInputAction.done,
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 15),
                              child: TextField(
                                controller: _passwordController,
                                obscureText: true,
                                decoration: InputDecoration(
                                    prefixIcon: Icon(Icons.lock),
                                    border: OutlineInputBorder(),
                                    labelText: '请输入密码',
                                    hintText: '请输入密码',
                                    contentPadding: EdgeInsets.all(0)),
                              ),
                            )
                          ],
                        ),
                      ),
                      actions: <Widget>[
                        FlatButton(
                          child: Text("取消"),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                        OutlineButton(
                          child: Text("登录"),
                          onPressed: () {
                            Navigator.pop(context,
                                "userName:${_controller.text},password:${_passwordController.text}");
                          },
                        )
                      ],
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)),
                    )).then((value) {
                  print(value);
                });
              },
            ),
            RaisedButton(
              color: Colors.yellow,
              child: Text("dialog2"),
              shape: RoundedRectangleBorder(
                  side: BorderSide(), borderRadius: BorderRadius.circular(30)),
              onPressed: () async {
                String content = await showDialog(
                    context: context,
                    child: SimpleDialog(
                      title: Text("请选择语言"),
                      children: <Widget>[
                        SimpleDialogOption(
                          child: Padding(
                            padding: EdgeInsets.all(10),
                            child: Text("简体中文"),
                          ),
                          onPressed: () {
                            Navigator.of(context).pop("简体中文");
                          },
                        ),
                        SimpleDialogOption(
                          child: Padding(
                            padding: EdgeInsets.all(10),
                            child: Text("繁体中文"),
                          ),
                          onPressed: () {
                            Navigator.of(context).pop("繁体中文");
                          },
                        )
                      ],
                    ));
                Fluttertoast.showToast(msg: content);
              },
            ),
            RaisedButton(
              color: Colors.green,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30)),
              child: Text('dialog3'),
              onPressed: () {
                showDialog(
                    context: context,
                    child: Dialog(
                      child: UnconstrainedBox(
                          constrainedAxis: Axis.vertical,
                          child: SizedBox(
                            width: 225,
                            height: 400,
                            child: Column(
                              children: <Widget>[
                                ListTile(
                                  title: Text("请选择"),
                                ),
                                Expanded(
                                  child: ListView.separated(
                                      itemBuilder: (context, index) {
                                        return ListTile(
                                          title: Text("第$index项"),
                                          onTap: () {
                                            Navigator.of(context).pop(index);
                                          },
                                        );
                                      },
                                      separatorBuilder: (context, index) {
                                        return Divider(
                                          color: Colors.green,
                                        );
                                      },
                                      itemCount: 100),
                                )
                              ],
                            ),
                          )),
                    )).then((onValue) {
                  Fluttertoast.showToast(msg: "选择了$onValue项");
                });
              },
            ),
            RaisedButton(
              color: Colors.orange,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30)),
              child: Text("dialog4(GeneralDialog)"),
              onPressed: () {
                showGeneralDialog(
                  context: context,
                  pageBuilder: (context, animation, animation2) {
                    return SafeArea(
                        child: AlertDialog(
                      title: Text('登录'),
                      content: Center(
                        heightFactor: 1,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            TextField(
                              controller: _controller,
                              decoration: InputDecoration(
                                  prefixIcon: Icon(Icons.accessibility),
                                  border: OutlineInputBorder(),
                                  labelText: '请输入用户名',
                                  hintText: '请输入用户名或手机号',
                                  contentPadding: EdgeInsets.all(0)),
                              autofocus: true,
                              textInputAction: TextInputAction.done,
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 15),
                              child: TextField(
                                controller: _passwordController,
                                obscureText: true,
                                decoration: InputDecoration(
                                    prefixIcon: Icon(Icons.lock),
                                    border: OutlineInputBorder(),
                                    labelText: '请输入密码',
                                    hintText: '请输入密码',
                                    contentPadding: EdgeInsets.all(0)),
                              ),
                            )
                          ],
                        ),
                      ),
                      actions: <Widget>[
                        FlatButton(
                          child: Text("取消"),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                        OutlineButton(
                          child: Text("登录"),
                          onPressed: () {
                            Navigator.pop(context,
                                "userName:${_controller.text},password:${_passwordController.text}");
                          },
                        )
                      ],
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)),
                    ));
                  },
                  barrierDismissible: true,
                  barrierLabel: "dialog",
                  barrierColor: Colors.greenAccent,
                  transitionDuration: const Duration(milliseconds: 150),
                  transitionBuilder: _buildMaterialDialogTransitions,
                );
              },
            ),
            RaisedButton(
              color: Colors.teal,
              child: Text("dialog5"),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30)),
              onPressed: () {
                showDialog(
                    context: context,
                    barrierDismissible: true,
                    child: AlertDialog(
                      title: Text('登录'),
                      content: Center(
                        heightFactor: 1,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            TextField(
                              controller: _controller,
                              decoration: InputDecoration(
                                  prefixIcon: Icon(Icons.accessibility),
                                  border: OutlineInputBorder(),
                                  labelText: '请输入用户名',
                                  hintText: '请输入用户名或手机号',
                                  contentPadding: EdgeInsets.all(0)),
                              autofocus: true,
                              textInputAction: TextInputAction.done,
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 15),
                              child: TextField(
                                controller: _passwordController,
                                obscureText: true,
                                decoration: InputDecoration(
                                    prefixIcon: Icon(Icons.lock),
                                    border: OutlineInputBorder(),
                                    labelText: '请输入密码',
                                    hintText: '请输入密码',
                                    contentPadding: EdgeInsets.all(0)),
                              ),
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                Text("记住密码"),
                                StatefulBuilder(builder: (context, _setState) {
                                  return Checkbox(
                                      value: _isSave,
                                      onChanged: (value) {
                                        _setState(() {
                                          _isSave = !_isSave;
                                        });
                                      });
                                }),
                              ],
                            ),
                          ],
                        ),
                      ),
                      actions: <Widget>[
                        FlatButton(
                          child: Text("取消"),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                        OutlineButton(
                          child: Text("登录"),
                          onPressed: () {
                            Navigator.pop(context,
                                "userName:${_controller.text},password:${_passwordController.text}");
                          },
                        )
                      ],
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)),
                    )).then((value) {
                  print(value);
                });
              },
            ),
            RaisedButton(
              child: Text('dialog6'),
              color: Colors.lightGreen,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30)),
              onPressed: () {
                showModalBottomSheet(
                    context: context,
                    builder: (context) {
                      return Column(
                        children: <Widget>[
                          ListTile(
                            title: Text("请选择"),
                          ),
                          Expanded(
                            child: ListView.separated(
                                itemBuilder: (context, index) {
                                  return ListTile(
                                    title: Text('第$index项'),
                                    leading: Image.asset('bizhi.jpg'),
                                  );
                                },
                                separatorBuilder: (context, index) {
                                  return Divider(
                                    color: Colors.deepOrange,
                                  );
                                },
                                itemCount: 200),
                          ),
                        ],
                      );
                    });
              },
            ),
            Builder(
                builder: (context) => RaisedButton(
                      child: Text('BottomSheet'),
                      color: Colors.purple,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(30)),
                      onPressed: () {
                        _persistentBottomSheetController =
                            Scaffold.of(context).showBottomSheet<String>((context) {
                          return Container(
                            height: 200,
                            color: Colors.green[50],
                            child: Column(
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    IconButton(icon: Icon(Icons.close), onPressed: () {
                                      Navigator.of(context).pop("aaaaaa");
                                    },)
                                  ],
                                  mainAxisAlignment: MainAxisAlignment.end,
                                )
                              ],
                            ),
                          );
                        });
                      },
                    )),
          ],
        ),
      ),
    );
  }

  Widget _buildMaterialDialogTransitions(
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child) {
    // 使用缩放动画
    return ScaleTransition(
      scale: CurvedAnimation(
        parent: animation,
        curve: Curves.easeOut,
      ),
      child: child,
    );
  }
}
